﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Drawing.Design;

namespace TA.Turbo.Controls
{
	public class HyperLinkNode : BaseTextNode
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public HyperLinkNode()
		{
			this.Href = "";
			this.OnClick = "";
			this.Target = "";
		}
		#endregion
		#region PROPS
		[Editor("System.Web.UI.Design.UrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
		[UrlProperty]
		[DefaultValue("")]
		[NotifyParentProperty(true)]
		public string Href { get; set; }
		[DefaultValue("")]
		[TypeConverter(typeof(System.Web.UI.WebControls.TargetConverter))]
		[NotifyParentProperty(true)]
		public string Target { get; set; }
		[DefaultValue("")]
		[NotifyParentProperty(true)]
		public string OnClick { get; set; }
		#endregion
		#endregion
		#region INTERNAL
		#region PROPS
		protected override TreeNodeType Type
		{
			get { return TreeNodeType.HyperLink; }
		}
		#endregion
		#region METHODS
		#region RENDER
		protected override void RenderContentsMain(TreeView owner, TreeNodePosition position, int level, HtmlTextWriter writer)
		{
			if (!string.IsNullOrEmpty(this.Href))
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Href, this.Href);
			}
			if (!string.IsNullOrEmpty(this.Target))
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Target, this.Target);
			}
			if (!string.IsNullOrEmpty(this.OnClick))
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Onclick, this.OnClick);
			}
			writer.RenderBeginTag(HtmlTextWriterTag.A);

			writer.WriteEncodedText(this.Text);
			
			writer.RenderEndTag();
		}
		
		#endregion
		#region JSON
		public override void SerializeContents(TA.JSON.JsonWriter jw)
		{
			base.SerializeContents(jw);

			if (!string.IsNullOrEmpty(this.Href))
			{
				jw.PropertyName("H");
				jw.Value(this.Href);
			}

			if (!string.IsNullOrEmpty(this.OnClick))
			{
				jw.PropertyName("O");
				jw.Value(this.OnClick);
			}

			if (!string.IsNullOrEmpty(this.Target))
			{
				jw.PropertyName("Ta");
				jw.Value(this.Target);
			}
		}
		public override void DeserializeContent(TA.JSON.IJsonReader jr, string propShortName)
		{
			switch (propShortName)
			{
				case "H": this.Href = (string)jr.Value; break;
				case "O": this.OnClick = (string)jr.Value; break;
				case "Ta": this.Target = (string)jr.Value; break;
				default:
					base.DeserializeContent(jr, propShortName);
					break;
			}
		}
		#endregion
		#endregion
		#endregion
	}
}
